<?php


namespace App\Models\Field;

use App\Consts\GlobalConst;
use App\Consts\GlobalUserConst;

class UserField extends BaseField
{
    public const ARTICLE_LANDING = 'articleLanding';

    public const SELF_INFO = 'selfInfo';

    public const COMMENT = 'comment';

    public const NOTE = 'note';

    public const DYNAMIC = 'dynamic';

    public const RELATIONSHIP = 'relationship';

    public const SALON = 'salon';

    public static function getFormats($scene = self::BASE_INFO): array

    {
        $baseFormats = [
            'id' => static function ($model) {
                return $model['user_id'];
            },
            'nickname',
            'avatar',
        ];
        $formats = [
            self::ARTICLE_LANDING => [
                'id' => static function ($model) {
                    return $model['user_id'];
                },
                'nickname',
                'avatar',
                'special_signature' => static function ($model) {
                    return $model['sign'];
                },
                'description'
            ],
            self::SALON => [
                'id' => static function ($model) {
                    return $model['user_id'];
                },
                'nickname',
                'avatar',
                'special_signature' => static function ($model) {
                    return $model['sign'];
                },
            ],
            self::SELF_INFO => [
                'id' => static function ($model) {
                    return $model['user_id'];
                },
                'nickname',
                'avatar',
                'is_fail' => static function ($model) {
                    return $model['status'] == GlobalUserConst::STATUS_DISABLE ||
                        $model['is_deleted'] == GlobalConst::IS_DELETED;
                },
                'is_new' => static function ($model) {
                    if ($model->nickname === '0') {
                        return false;
                    }

                    return !$model->nickname;
                },
                'created_at',
            ],

            self::BASE_INFO => [
                'id' => static function ($model) {
                    return $model['user_id'];
                },
                'nickname',
                'avatar',
            ],
            self::SELF_LIST => array_merge($baseFormats, [
                'special_signature' => static function ($model) {
                    return $model['sign'];
                },
            ]),
            self::SELF_DETAIL => [
                'id' => static function ($model) {
                    return $model['user_id'];
                },
                'phone' => static function ($model) {
                    return empty($model['phone']) ? '' : substr_replace($model['phone'], '****', 3, 4);
                },
                'identity',
                'nickname',
                'avatar',
                'status',
                'special_signature' => static function ($model) {
                    return $model['sign'];
                },
                'birthday',
                'gender',
                'description',
                'province',
                'city',
                'industry' => static function ($model) {
                    if (!$model['industry_id'] || !$model['industry_parent_id']) {
                        return [];
                    }
                    return explode(',', $model['industry_parent_id'] . ',' . $model['industry_id']);
                },
                'job' => static function ($model) {
                    if (!$model['job_id'] || !$model['job_parent_id']) {
                        return [];
                    }
                    return explode(',', $model['job_parent_id'] . ',' . $model['job_id']);
                },
                'education' => static function ($model) {
                    return $model['education_id'];
                },
                'is_new' => static function ($model) {
                    if ($model->nickname === '0') {
                        return false;
                    }

                    return !$model->nickname;
                },
                'created_at'
            ],
            self::COMMENT => [
                'id' => static function ($model) {
                    return $model->user_id;
                },
                'nickname',
                'avatar',
            ],
            self::NOTE => [
                'id' => static function ($model) {
                    return $model->user_id;
                },
                'nickname',
                'avatar',
            ],
            self::DYNAMIC => [
                'id' => static function ($model) {
                    return $model->user_id;
                },
                'nickname',
                'avatar',
            ],
            self::RELATIONSHIP => [
                'id' => static function ($model) {
                    return $model->user_id;
                },
                'nickname',
                'avatar',
            ],
        ];

        return $formats[$scene] ?? $baseFormats;
    }
}
